<?php
namespace app\super\controller;
use think\Db;
use think\Controller;
use  think\Request;

class Mechanism extends Base
{
	public function Index(){
		
		
		
	
		
		 return view();
	}
	
	
	
	function Add(){
		
		$request = Request::instance();
	
		
		if($request->isPost()){
				$data["id"] =$request->post('id');
			
				$data["name"] =$request->post('name');
				$data["truename"] =$request->post('truename');
				$data["opentype"] =$request->post('opentype');
				$data["desc"] =$request->post('desc');
			
				$data["endtime"] =$request->post('endtime');
				$data["mtype"] =$request->post('mtype');
				$data["address"] =$request->post('address');
			
				$data["dailiid"] =$request->post('dailiid');
				$data["ip"] =$request->post('ip');
				
				
			if($data["id"]){
				if(db('mechanism')->update($data)){
					
					  return ['status'=>200,'message'=>'修改成功'];
				}else{
					 return ['status'=>500,'message'=>'修改失败'];
				}
			}else{
				$data["jointime"] = date("Y-m-d h:i:sa");
				$data["province"] =$request->post('province');
				$data["city"] =$request->post('city');
				$data["area"] =$request->post('area');
			  $data["phone"] =$request->post('phone');
			  $data["number"] =$request->post('number');
			  $data["createtime"] =  date("Y-m-d h:i:sa");
			  $data["state"] =$request->post('state');

			  $mechanism = db("mechanism")->where("name='".$data["name"]."'")->find();
			  $mechanism2 = db("mechanism")->where("phone='".$data["phone"]."'")->find();
			  if($mechanism){
				  return json(['status'=>500,'message'=>'机构名称重复了']);
			  }else if($mechanism2){
				  return json(['status'=>500,'message'=>'联系方式重复了']);

			  }else{

			
				$mechanism =db('mechanism')->where("province='".$data["province"]."' and city='".$data["city"]."' and area='".$data["area"]."'")->order("id desc")->find();
				if($mechanism){
				  $number = $mechanism["number"]+1;
				  $data["number"]=$number;
				}else{
					$data["number"]= $data["number"]."1";
				}
			   
				$mid = db('mechanism')->insertGetId($data);
				if($mid){
				  $user["username"] = $data["number"];
				  $password = mt_rand(10000, 99999);
				  $user["password"] = md5($password);
				  $user["state"] ="1";
				  $jiaoseinfo = db("sys_jiaose")->where("jiaosename='管理员'")->find();
				  $user["jiaoseid"] =$jiaoseinfo["id"];
				  $user["mid"] = $mid;
				  $user["createtime"] =  date("Y-m-d h:i:sa");
				  $user["type"] = "1";
				  db('manage')->insertGetId($user);
				  $templatecode="SMS_152856914";
				  
				  $params = [$data["name"],$data["number"],$password];
				  sendsms($data["phone"],"",$templatecode,"1",$params);

					//初始化角色
				  $insertjiaose = Db::execute("insert into sys_jiaose(jiaosename,backinfo,quanxian,state,aid) select jiaosename,backinfo,quanxian,1,".$mid." from sys_jiaose where jiaosename='教师' and aid is null",[]);
					//初始化配置信息
					$sys_webconfig= Db::execute("insert into sys_webconfig(signnum,signjgtime,mid) values(?,?,?) ",[1,1,$mid]);
					$food= Db::execute("insert into food(title,mid,createtime,type,mydate) values(?,?,?,?,?) ",["今日美食",$data["id"],date("Y-m-d h:i:sa"),"早餐",date("Y-m-d h:i:sa")]);
  
					   return json(['status'=>200,'message'=>'添加成功']);
					  
				  }else{
					  return json(['status'=>500,'message'=>'修改失败']);
				  }

				}
			
			 
			}
			
		}else{
			
			if($request->get('id')){
				$info =db('mechanism')->where("id=".$_GET["id"])->find();
				$this->assign('info',$info);
				
			}
		
			
		
			
		}
		
		
	
	
		return view();
	}
	 public function GetMechanism(){

		
		$where=" 1=1 ";

		if($_POST["stime"]&&$_POST["etime"]){
			$where=$where." and endtime between '".$_POST["stime"]."' and '".$_POST["etime"]."'";
		}
		if($_POST["truename"]){
			$where=$where." and truename like '%".$_POST["truename"]."%'";
		}	
		if($_POST["name"]){
			$where=$where." and name like '%".$_POST["name"]."%'";
		}
		if($_POST["state"]!=""){
			$where=$where." and state=".$_POST["state"];
		}
		if($_POST["daoqi"]){
			$where=$where." and endtime between now() and date_add(now(),interval ".$_POST["daoqi"]." month) and state=1 ";
		}
		if($_POST["dailiid"]){
			$where=$where." and dailiid=".$_POST["dailiid"];
		}

	
		$count =db('mechanism')->where($where)->count();
		$first = $_POST["size"] * ($_POST["page"] -1);
		 
		$list = db('mechanism')->where($where)->order("id desc")->limit($first,$_POST["size"])->select();
		foreach ($list as $key => &$value) {

			if($value["state"]!="0"){
				$endtime = $value["endtime"];
				if(strtotime($endtime)>time('Y-m-d')){
					Db::execute('update mechanism set state=1 where id=?',[$value["id"]]);
					$value["state"]="1";
			   }else{
				Db::execute('update mechanism set state=-1 where id=?',[$value["id"]]);
				$value["state"]="-1";
			   }

			}
		

		}
		if($count%$_POST["size"]==0){
			$obj["count"]=(int)($count/$_POST["size"]);
		}else{
			$obj["count"]=(int)($count/$_POST["size"])+1;
		}

	

		$obj["allcount"]=$count;
		$obj["list"]=$list;
		return json($obj);
		
	}
	 public function GetMechanismList(){
		$where="";
		
		$list = db('mechanism')->where("")->select();

		return json($list);
		
	}
	public function GetMechanismb_daili(){
		$where="dailiid=".$_POST["id"];
		
		$list = db('mechanism')->where($where)->select();
		return json($list);
		
	}
	function test(){

		$id=51;
		$list=db('student_card')->where("mid=".$id)->delete();

		echo $list;
	}
	function Del(){
		$request = Request::instance();
		$id =$request->post('id');
		
		
		 
		   $where = 'id='.$id;
		   $list=db('manage')->where("mid=".$id)->delete();
		   $list=db('food')->where("mid=".$id)->delete();
		   $list=db('imagelist')->where("mid=".$id)->delete();
		   $list=db('leave')->where("mid=".$id)->delete();
		   $list=db('myclass')->where("mid=".$id)->delete();
		   $list=db('news')->where("mid=".$id)->delete();
		   $list=db('sign')->where("mid=".$id)->delete();
		   $list=db('student')->where("mid=".$id)->delete();
		   $list=db('studentandparent')->where("mid=".$id)->delete();
		   $list=db('teacher')->where("mid=".$id)->delete();
		   $list=db('teacherandclass')->where("mid=".$id)->delete();
		   $list=db('teacheranduser')->where("mid=".$id)->delete();
		   $list=db('video')->where("mid=".$id)->delete();
		   $list=db('work')->where("mid=".$id)->delete();
		   $list=db('foodstar')->where("mid=".$id)->delete();
		   $list=db('imagelist')->where("mid=".$id)->delete();
		   $list=db('mechanismorder')->where("mid=".$id)->delete();

		   $list=db('message')->where("mid=".$id)->delete();

		   $list=db('messagelist')->where("mid=".$id)->delete();

		   $list=db('studentvideo')->where("mid=".$id)->delete();

		   $list=db('student_card')->where("mid=".$id)->delete();
		 
		   $list=db('users')->where("mid=".$id)->delete();

		   $list=db('sys_webconfig')->where("mid=".$id)->delete();
		   
		   $list=db('mechanism')->where($where)->delete();

		   if($list!==false) {
			 return ['status'=>200,'message'=>'删除成功'];
		   }else{
			  return ['status'=>500,'message'=>'删除失败'];
		   }
		
	}

		
	function dailiadd(){
		
		$request = Request::instance();
	
		
		if($request->isPost()){
				$data["id"] =$request->post('id');
			
				$data["name"] =$request->post('name');
				$data["truename"] =$request->post('truename');
				$data["phone"] =$request->post('phone');
				$data["weixin"] =$request->post('weixin');
				$data["zhifubao"] =$request->post('zhifubao');
				$data["kaihuhang"] =$request->post('kaihuhang');
				$data["kahao"] =$request->post('kahao');
				$data["createtime"] = date("Y-m-d H:i:sa");
				$data["model1"] =$request->post('model1');
				$data["model2"] =$request->post('model2');
			
				
			if($data["id"]){
				if(db('daili')->update($data)){
					
					  return ['status'=>200,'message'=>'修改成功'];
				}else{
					 return ['status'=>500,'message'=>'修改失败'];
				}
			}else{

				$data["mymoney"] = 0;

				$daili = db("daili")->where("phone='".$data["phone"]."'")->find();
				if($daili){
					return ['status'=>500,'message'=>'手机号码重复'];
				}

				$mid = db('daili')->insertGetId($data);
				if($mid){

					$user["username"] = $data["phone"];
					$password = mt_rand(10000, 99999);
					$user["password"] = md5($password);
					$user["state"] ="1";
					$user["isdaili"] ="1";
	
					$jiaoseinfo = db("sys_jiaose")->where("jiaosename='代理商'")->find();
					$user["jiaoseid"] =$jiaoseinfo["id"];
					$user["mid"] = 0;
					$user["createtime"] = date("Y-m-d H:i:sa");
					$user["type"] = "1";
					$user["issuper"] = 0;
					$user["dailiid"] =$mid;
					db('manage')->insertGetId($user);

					$templatecode="SMS_152856914";
				  
					$params = [$data["name"],$data["phone"],$password];
					sendsms($data["phone"],"",$templatecode,"1",$params);




					   return json(['status'=>200,'message'=>'添加成功']);
					  
				  }else{
					  return json(['status'=>500,'message'=>'修改失败']);
				  }

			  
			
			 
			}
			
		}else{
			
			if($request->get('id')){
				$info =db('daili')->where("id=".$_GET["id"])->find();
				$this->assign('info',$info);
				
			}
		
			
		
			
		}
		
		
	
	
		return view();
	}
	public function daili(){

		return view();
	}
	public function mechanismdaili(){

		$this->assign('id',$_GET["id"]);
		return view();
	}
	public function indexbydaili(){
		$id = session("dailiid");
		$this->assign('id',$id);
		return view();
	}
	 public function GetDaili(){
		$where="1=1";
		if($_POST["name"]){
			$where=$where." and name like '%".$_POST["name"]."%'";
		}
		if($_POST["truename"]){
			$where=$where." and truename like '%".$_POST["truename"]."%'";
		}
		if($_POST["phone"]){
			$where=$where." and phone like '%".$_POST["phone"]."%'";
		}
		$count =db('daili')->where($where)->count();
		$first = $_POST["size"] * ($_POST["page"] -1);
		 
		$list = db('daili')->where($where)->limit($first,$_POST["size"])->select();
		foreach ($list as $key => &$value) {
			$num = db("mechanism")->where("dailiid=".$value["id"]." and state=1")->count();
			$value["num"]=$num;
		}
		$obj["count"]=(int)($count/$_POST["size"])+1;
		$obj["allcount"]=$count;
		$obj["list"]=$list;
		return json($obj);
		
	}
	public function setmoney(){
			if($_POST){
				$data["id"]=$_POST["id"];
				$data["model1"]=$_POST["model1"];
				$data["model2"]=$_POST["model2"];
				if(db('mechanism')->update($data)){
 						return ['status'=>200,'message'=>'操作成功'];
				}
			}else{

				$info = db("mechanism")->where("id=".$_GET["mid"])->find();
				$this->assign('info',$info);
				return view();

			}

	}
	 public function GetDailiList(){
	

		$where="";
		
		$list = db('daili')->where("")->select();
		
		return json($list);
		
	}
	
	function DaiLiDel(){
		$request = Request::instance();
		$id =$request->post('id');
		
		
		 
		   $where = 'id='.$id;
	
		   $list=db('daili')->where($where)->delete();
		   if($list!==false) {
			 return ['status'=>200,'message'=>'删除成功'];
		   }else{
			  return ['status'=>500,'message'=>'删除失败'];
		   }
		
	}
	
	function sh(){
		$request = Request::instance();


		if($request->isPost()){
			$data["id"]=$request->post('id');

			$mechanism= db("mechanism")->where("id=".$data["id"])->find();

			$data["opentype"]=$request->post('opentype');
			$data["dailiid"]=$request->post('dailiid');
			$data["mtype"]=$request->post('mtype');
			$month=$request->post('month');


			$time= date("Y-m-d");
			$data["endtime"] = date("Y-m-d",strtotime("+".$month." month",strtotime($time)));
			$data["state"]="1";
			$data["jointime"] = date("Y-m-d H:i:s");

			if(db('mechanism')->update($data)){

				$user["username"] = $mechanism["number"];
				$password = mt_rand(10000, 99999);
				$user["password"] = md5($password);
				$user["state"] ="1";
				$jiaoseinfo = db("sys_jiaose")->where("jiaosename='管理员'")->find();
				$user["jiaoseid"] =$jiaoseinfo["id"];
				$user["mid"] = $data["id"];
				$user["createtime"] =  date("Y-m-d H:i:sa");
				$user["type"] ="1";
				db('manage')->insertGetId($user);

				//角色初始化
				$insertjiaose = Db::execute("insert into sys_jiaose(jiaosename,backinfo,quanxian,state,aid) select jiaosename,backinfo,quanxian,1,".$data["id"]." from sys_jiaose where jiaosename='教师' and aid is null",[]);

				//初始化配置信息
				$sys_webconfig= Db::execute("insert into sys_webconfig(signnum,signjgtime,mid) values(?,?,?) ",[1,1,$data["id"]]);

				$food= Db::execute("insert into food(title,mid,createtime,type,mydate) values(?,?,?,?,?) ",["今日美食",$data["id"],date("Y-m-d h:i:sa"),"早餐",date("Y-m-d h:i:sa")]);

				//短信推送
				$templatecode="SMS_152856914";

				$params = [$mechanism["name"],$mechanism["number"],$password];
				sendsms($mechanism["phone"],"",$templatecode,"1",$params);

				//微信推送
				$openid = $mechanism["openid"];
				if($openid!=""){
					$arr[0]="入驻小不点乐学";
					$arr[1]=$mechanism["createtime"];
					$arr[2]=$mechanism["jointime"];
					$arr[3]="";
					$arr[4]="";

					$title="恭喜您！您已入驻成功！机构名称：".$mechanism["name"]."编号：".$mechanism["number"]."登录密码：".$password." ，请尽快登录平台进行修改密码。";
					sendtempmsg2("vC1ssnSjRK_toN0V9G4fPNozKXXhCIJwMNhX5YxsMhU",$openid,$title,$arr);
				}
				



				return ['status'=>200,'message'=>'审核成功'];
		  }else{
			   return ['status'=>500,'message'=>'审核失败'];
		  }

		}else{

			$id =$request->get('id');
			$mechanism = db("mechanism")->where("id=".$id)->find();
			$this->assign('info',$mechanism);
			return view();
		}
	
	}
	function chongzhi(){
		$id =$_GET["id"];
		$mid =$_GET["mid"];

		$this->assign('mid',$mid);
		$this->assign('id',$id);
		return view();
	}
	function chongzhiadd(){

			$data["dailiid"]=$_POST["dailiid"];
			$data["money"]=$_POST["money"];
			$data["createtime"]= date("Y-m-d H:i:sa");
			$data["dailiid"]=$_POST["dailiid"];
			$data["mid"]=$_POST["mid"];

			if($data["mid"]){
				//给机构充值
				$check = db("daili")->where("id=".$data["dailiid"]." and mymoney>".$data["money"])->find();
				if($check){
						$info = db("dailiorder")->insertGetId($data);
					//	$result= Db::execute("update daili set mymoney=mymoney-? where id=?",[$data["money"],$data["dailiid"]]);
						$result= Db::execute("update mechanism set mymoney=mymoney+? where id=?",[$data["money"],$data["mid"]]);

						if($info&&$result){
							return ['status'=>200,'message'=>'充值成功'];
						}else{
							return ['status'=>400,'message'=>'充值失败'];
						}

				}else{
					return ['status'=>400,'message'=>'余额不足'];
				}
			}else if($data["dailiid"]){
				$info = db("dailiorder")->insertGetId($data);
				$result= Db::execute("update daili set mymoney=mymoney+? where id=?",[$data["money"],$data["dailiid"]]);
				if($info&&$result){
					return ['status'=>200,'message'=>'充值成功'];
				}else{
					return ['status'=>400,'message'=>'充值失败'];
				}

			}


	}
	function chongzhilist(){
		$id =$_GET["id"];
		$mid =$_GET["mid"];
	
			$this->assign('id',$id);
	
			$this->assign('mid',$mid);
		
		return view();
	}
	function getchongzhilist(){
		$request = Request::instance();
		$dailiid =$request->post('dailiid');
		$mid =$request->post('mid');
		if($dailiid){
			$where="dailiid=".$dailiid;
		}else if($mid){
			$where="mid=".$mid;
		}
		
	
		$count =db('dailiorder')->where($where)->count();
		$first = $_POST["size"] * ($_POST["page"] -1);
		 
		$list = db('dailiorder')->where($where)->order("id desc")->limit($first,$_POST["size"])->select();
		
		$obj["count"]=(int)($count/$_POST["size"])+1;
		$obj["allcount"]=$count;
		$obj["list"]=$list;
		return json($obj);

	}

	function xuqi(){
		$request = Request::instance();


		if($request->isPost()){
			$data["id"]=$request->post('id');
			$month=$request->post('month');
			$money=$request->post('money');

			$mechanism= db("mechanism")->where("id=".$data["id"])->find();
			$endtime = $mechanism["endtime"];
			$time="";
			if(strtotime($endtime)<time('Y-m-d')){
				$time= date("Y-m-d");
			}else{
				$time = $endtime;
			}
			
			$data["endtime"] =date("Y-m-d",strtotime("+".$month." month",strtotime($time)));


			//file_put_contents("test.txt","endtime".$data["endtime"].PHP_EOL,FILE_APPEND);
		
			
			if(db('mechanism')->update($data)){

				$ss["month"]=$month;
				$ss["createtime"]=date("Y-m-d H:i:s");
				$ss["mid"]=$data["id"];
				$ss["money"]=$money;
				$ss["endtime"]=$data["endtime"];

				if(db("mechanismorder")->insertGetId($ss)){


					//微信推送
					$openid = $mechanism["openid"];
					if($openid!=""){
						$arr[0]=$mechanism["number"];
						$arr[1]=$money."元";
						$arr[2]=$data["endtime"];

						$title="尊敬的用户，您已成功续费";
						sendtempmsg2("tiYW2cF880Bb4MSn0YBq-0dlrLvsDZhU4C5e1vVh8zU",$openid,$title,$arr);
					}

					//短信推送
					$templatecode="SMS_157453486";
					$params = [$data["endtime"]];
					sendsms($mechanism["phone"],"",$templatecode,"1",$params);

					return ['status'=>200,'message'=>'续期成功'];
				}else{
					return ['status'=>500,'message'=>'续期失败'];
				}


				
		  }else{
			   return ['status'=>500,'message'=>'续期失败'];
		  }

		}else{

			$id =$request->get('id');
			$mechanism = db("mechanism")->where("id=".$id)->find();
			$this->assign('info',$mechanism);
			return view();
		}
	
	}
	function setkefu(){
		$request = Request::instance();
		$idarr=$request->post('idarr');
		$state=$request->post('state');

		$updateresult = Db::execute('update users set iskefu=? where id in (?)',[$state,$idarr]);
		if($updateresult){
			$obj["status"]=200;
			$obj["message"]="操作成功";
			return json($obj);
		}else{
			$obj["status"]=400;
			$obj["message"]="操作失败";
			return json($obj);
		}

	}
	function video(){
		$request = Request::instance();
		$id=$request->get('id');

		$videos= db("video")->where("mid=".$id)->select();
		$sys_webconfig = db("sys_webconfig")->where("mid=0")->find();


		foreach ($videos as $key => &$value) {
			$mechanism = db("mechanism")->where("id=".$value["mid"])->find();
		
			$value["rtmpurl"]=getPushUrl($sys_webconfig["push"], $value["videourl"], $sys_webconfig["apikey"], "2050-01-01 23:59:59");
			// $play = getPlayUrl($sys_webconfig["cdn"], $value["videourl"]);
			// $value["url"]=$play[0];
			$time = date("Y-m-d H:i:s",time()+7200);
			$play = getplayurl2($sys_webconfig["cdn"], $value["videourl"], $sys_webconfig["playapikey"], $time);
			$value["url"]=$play[2];

		}
		$this->assign('mid',$id);
		$this->assign('videos',$videos);
		return view();
	}
	function fpvideo(){
		$dailiid = session("dailiid");
		$this->assign('dailiid',$dailiid);

		return view();
	}
	public function fenpei(){

		if($_POST){
			$videoid =$_POST["videoid"];
			$mid =$_POST["mid"];
			$insertjiaose = Db::execute("update video set mid =".$mid." where id=".$videoid,[]);
			return ['status'=>200,'message'=>'分配成功'];

		}else{
			$dailiid = session("dailiid");

			$this->assign('dailiid',$dailiid);
			$this->assign('id',$_GET["id"]);
			return view();

		}
	
	}
	public function videoadd(){
		$request = Request::instance();
		if($request->isPost()){
				$data["id"] =$request->post('id');
				$data["videourl"] =$request->post('videourl');
				$data["createtime"] = date('Y-m-d H:i:s',time());
				$data["state"] =$request->post('state');
				$data["dailiid"] =$request->post('dailiid');
			if($data["id"]){
				if(db('video')->update($data)){
					  return ['status'=>200,'message'=>'修改成功'];
				}else{
					 return ['status'=>500,'message'=>'修改失败'];
				}
			}else{
				if(db("video")->where("videourl='".$data["videourl"]."'")->find()){

					return ['status'=>500,'message'=>'摄像头编号重复了'];
				}
			  if(db('video')->insertGetId($data)){
					 return ['status'=>200,'message'=>'添加成功'];
					
				}else{
					return ['status'=>500,'message'=>'修改失败'];
				}
			}
		}else{
			if($request->get('id')){
				$info =db('video')->where("id=".$_GET["id"])->find();
				$this->assign('info',$info);
			}
			if($_GET["mid"]){
				$this->assign('mid',$_GET["mid"]);
			}
			return view();
		}
	}
	public function videoadd2(){
		$request = Request::instance();
	
	
		if($request->isPost()){
				$data["id"] =$request->post('id');
				$data["title"] =$request->post('title');
				$data["desc"] =$request->post('desc');
		
				$data["seq"] =$request->post('seq');
				$data["createtime"] = date('Y-m-d H:i:s',time());
			
				$data["state"] =$request->post('state');
				$data["imgurl"] =$request->post('imgurl');
				$data["stime"] =$request->post('stime');
				$data["etime"] =$request->post('etime');
				$data["classid"] =$request->post('classid');
				
			if($data["id"]){
				
			
				
				if(db('video')->update($data)){
					
					  return ['status'=>200,'message'=>'修改成功'];
				}else{
					 return ['status'=>500,'message'=>'修改失败'];
				}
			}else{

				$data["videourl"] =$request->post('videourl');
				$data["mid"] = $request->post('mid');
				
				if(db("video")->where("videourl='".$data["videourl"]."'")->find()){

					return ['status'=>500,'message'=>'摄像头编号重复了'];
				}

			
			  if(db('video')->insertGetId($data)){
					 return ['status'=>200,'message'=>'添加成功'];
					
				}else{
					return ['status'=>500,'message'=>'修改失败'];
				}
			}
			
		}else{
			
			if($request->get('id')){
				$info =db('video')->where("id=".$_GET["id"])->find();
				$this->assign('info',$info);
				
			}
			if($_GET["mid"]){
				$this->assign('mid',$_GET["mid"]);
			}
		
			return view();
		
			
		
			
		}
	}
	function order(){
		$request = Request::instance();
		$id =$request->get('id');

		$this->assign('mid',$id);
		return view();
	}
	function getorderlist(){
		$request = Request::instance();
		$mid =$request->post('mid');
		$where="mid=".$mid;
		$count =db('mechanismorder')->where($where)->count();
		$first = $_POST["size"] * ($_POST["page"] -1);
		 
		$list = db('mechanismorder')->where($where)->order("id desc")->limit($first,$_POST["size"])->select();
		
		$obj["count"]=(int)($count/$_POST["size"])+1;
		$obj["allcount"]=$count;
		$obj["list"]=$list;
		return json($obj);

	}
	function orderdel(){
		$request = Request::instance();
		$id =$request->post('id');

		$where="id=".$id;
		$list=db('mechanismorder')->where($where)->delete();
		if($list!==false) {
			return ['status'=>200,'message'=>'操作成功'];
		  }else{
			 return ['status'=>500,'message'=>'操作失败'];
		  }
		
	}

	function daililistseq(){
		$result = Db::query('select *,(select count(*) from mechanism where dailiid=daili.id and state=1) as num from daili order by num desc',[]);
		return json($result);
	}

	function jigouaddresstj(){
		$result = Db::query('SELECT province,count(*) as num FROM mechanism where state=1 group by province',[]);
		return json($result);
	}
	

	function exportmechanism(){

		$where=" 1=1 ";

		if($_GET["stime"]&&$_GET["etime"]){
			$where=$where." and endtime between '".$_GET["stime"]."' and '".$_GET["etime"]."'";
		}
		if($_GET["truename"]){
			$where=$where." and truename like '%".$_GET["truename"]."%'";
		}	
		if($_GET["name"]){
			$where=$where." and name like '%".$_GET["name"]."%'";
		}
		if($_GET["state"]!=""){
			$where=$where." and state=".$_GET["state"];
		}
		if($_GET["daoqi"]){
			$where=$where." and endtime between now() and date_add(now(),interval ".$_GET["daoqi"]." month) and state=1 ";
		}
		 
		$list = db('mechanism')->where($where)->order("id desc")->limit($first,$_POST["size"])->select();
		vendor('PHPExcel.PHPExcel');
        //3.实例化PHPExcel类
        $objPHPExcel = new \PHPExcel();
        //4.激活当前的sheet表
        $objPHPExcel->setActiveSheetIndex(0);
        //5.设置表格头（即excel表格的第一行）
        $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValue('A1', '机构编号')
                ->setCellValue('B1', '机构名称')
				->setCellValue('C1', '联系人')
				->setCellValue('D1', '联系电话')
				->setCellValue('E1', '区域')
				->setCellValue('F1', '加入时间')
				->setCellValue('G1', '到期时间');
			
      
        //6.循环刚取出来的数组，将数据逐一添加到excel表格。
        for($i=0;$i<count($list);$i++){
            $objPHPExcel->getActiveSheet()->setCellValue('A'.($i+2),$list[$i]['number']);//ID
            $objPHPExcel->getActiveSheet()->setCellValue('B'.($i+2),$list[$i]['name']);//标签码
			$objPHPExcel->getActiveSheet()->setCellValue('C'.($i+2),$list[$i]['truename']);//防伪码
			$objPHPExcel->getActiveSheet()->setCellValue('D'.($i+2),$list[$i]['phone']);//防伪码
			$objPHPExcel->getActiveSheet()->setCellValue('E'.($i+2),$list[$i]['province']."-".$list[$i]['city']."-".$list[$i]['area']);//防伪码
			$objPHPExcel->getActiveSheet()->setCellValue('F'.($i+2),$list[$i]['jointime']);//防伪码
			$objPHPExcel->getActiveSheet()->setCellValue('G'.($i+2),$list[$i]['endtime']);//防伪码
        }
        //7.设置保存的Excel表格名称
        $filename = '机构信息'.date('ymd',time()).'.xls';
        //8.设置当前激活的sheet表格名称；
        $objPHPExcel->getActiveSheet()->setTitle('机构信息');
				//9.设置浏览器窗口下载表格
				ob_end_clean();
        header("Content-Type: application/force-download");  
        header("Content-Type: application/octet-stream");  
        header("Content-Type: application/download");  
        header('Content-Disposition:inline;filename="'.$filename.'"');  
        //生成excel文件
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        //下载文件在浏览器窗口
		$objWriter->save('php://output');
		
	}
	
}